﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using RSA;
using Utils;
using System.Text;

namespace UnionPay4WP
{
    public class UPPayRsa
    {
        //public bool RsaEncrypt(string sExponent, string sModulus, string sInput, string sOutput)
        public bool RsaEncrypt(string sExponent, string sModulus, byte[] sInput, out string sOutput)
        {
            sOutput = "";
            try
            {
                //93716304585135386147007456346495207974915681551681333010606143979751660657428852845087672310448592787191681231125442581642251207627443243638648672776016343668644156640389200758863624667407953984249079575910993624091568988262627752574106503927829967495707880595781666554070793222145163225943407723575126201759
                //937163045851353861470074563464952079749156815516881231125442581642251207627443243638648672776016343668644156640389200758863624667407953984249079575910993624091568988262627752574106503927829967495707880595781666554070793222145163225943407723575126201759
                //89979131010449733055731466340030907952127252056298539795841872593459072051342594531780082610242068977643798454881825942618699992740578749483225107013308859741019218001531035719593250396156028152262862911046028991221178995860352661026311379005297199898136068198714959613491299221573590319458982292437895709103", 10);
                //BigInteger exponent = new BigInteger("65537", 10);
                BigInteger modulus = new BigInteger(sModulus, 10);
                BigInteger exponent = new BigInteger(sExponent, 10);

                RSAParameters rsaParameters = new RSAParameters()
                {
                    //Exponent = Encoding.UTF8.GetBytes(exponent), // new byte[] {01, 00, 01}
                    E = exponent.getBytesRaw(), // FromHex(exponent),
                    // Modulus = FromHex(modulus),   // new byte[] {A3, A6, ...}
                    N = modulus.getBytesRaw(),//Encoding.UTF8.GetBytes(modulus),
                    // D = privateEx.getBytesRaw(),
                };
                RSACrypto rsa = new RSACrypto();
                rsa.PaddingProvider = new RSA.PaddingProviders.PKCS1v1_5();
                rsa.ImportParameters(rsaParameters);
                byte[] input = sInput;//Encoding.UTF8.GetBytes(sInput);
                byte[] bOutput = rsa.Encrypt(input);
                sOutput = DES3.byteToHexStr(bOutput);//BitConverter.ToString(bOutput).Replace("-", string.Empty);
            }
            catch
            {
                return false;
            }
            return true;

        }
    }
}
